
#import "MMExampleLeftSideDrawerViewController.h"
#import "UIViewController+MMDrawerController.h"

#import "UITableViewCell+FlatUI.h"
#import "tooles.h"
#import "AppDelegate.h"
#import <QuartzCore/QuartzCore.h>
#import "BigAboutUsViewController.h"
#import "SMMNavigationController.h"
#import "YijingMainViewController.h"

@interface MMExampleLeftSideDrawerViewController () <UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

@property(nonatomic,strong) UIImageView *bgView;
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) YijingMainViewController *mainController;
@property(nonatomic,strong) BigAboutUsViewController *aboutController;

@end

@implementation MMExampleLeftSideDrawerViewController

@synthesize tableView=_tableView;

@synthesize mainController = _mainController;
@synthesize aboutController = _aboutController;

-(id)init{
    self = [super init];
    if(self){
    }
    return self;
}

-(void)viewDidLoad{
    
    [super viewDidLoad];
    
    self.bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cover.jpg"]];
    self.bgView.contentMode = UIViewContentModeScaleToFill;
    [self.view addSubview:self.bgView];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, MyDrawerDefaultWidth, self.view.frame.size.height - 50) style:UITableViewStylePlain];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5];
    _tableView.backgroundView = nil;
    [self.view addSubview:self.tableView];
    
    self.mainController = [[YijingMainViewController alloc] init];
    _mainController.title = @"百家姓";
    
    self.aboutController = [[BigAboutUsViewController alloc] init];
    _aboutController.title = @"使用帮助";
    
    [self centerDrawerChanged:_mainController];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    WS(ws);
    //TODO use masonry to re-frame all the subviews.
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(ws.view);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(ws.view);
        make.left.mas_equalTo(ws.view);
        make.top.mas_equalTo(ws.view).with.offset(50);
        make.bottom.mas_equalTo(ws.view);
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 2;
        default:
            return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kDeviceWidth/4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    static NSString *inde = @"LeftDrawerID";
    cell = [tableView dequeueReusableCellWithIdentifier:inde];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"LeftDrawerCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    UIImageView *lab1 = (UIImageView*)[cell viewWithTag:1];
    switch (indexPath.row) {
        case 0:
            lab1.image = [UIImage imageNamed:@"style2_button_catalog_normal.png"];
            break;
        case 1:
            lab1.image = [UIImage imageNamed:@"style2_button_help_normal.png"];
            break;
        default:
            break;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self drawChanged:indexPath.row + 1];
}

-(void)drawChanged:(int)aIndex
{
    UIViewController *controller;
    switch (aIndex) {
        case 1:
        {
            controller = _mainController;
        }
            break;
        case 2:
        {
            controller = _aboutController;
        }
            break;
        default:
            break;
    }
    
    [self centerDrawerChanged:controller];
}

-(void)centerDrawerChanged:(UIViewController *)aVC
{
    SMMNavigationController *nav = [[SMMNavigationController alloc] initWithRootViewController:aVC];
    aVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"功能" style:UIBarButtonItemStylePlain target:self action:@selector(pressedLeftButton)];
    [self.mm_drawerController setCenterViewController:nav withCloseAnimation:YES completion:nil];
}

-(void)pressedLeftButton
{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}

- (BOOL)shouldAutorotate
{
    return YES;
}

@end
